<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>核销码管理</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../include/commonFile.jsp" %>
    <style type="text/css">
        .layui-table td, .layui-table th {
            padding-left: 10px !important;
            padding-right: 10px !important;
        }
    </style>
</head>
<body>
<!--头部-->
<%@include file="../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <form class="layui-form" action="${ctx}/coupon/couponCode/list.do?couponId=${coupon.id}" id="myForm" method="post">
                <input type="hidden" name="pageNo" id="pageNo" value="${page.page}"/>
                <div class="f-search-bar">
                    <div class="search-container">
                        <ul class="search-form-content">
                            <li class="form-item-inline"><label class="search-form-lable">核销码</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="verifyCode" autocomplete="off" class="layui-input"
                                           value="${couponCode.verifyCode}" placeholder="请输入查询核销码">
                                </div>
                            </li>
                            <li class="form-item-inline"><label class="search-form-lable">领取状态</label>
                                <div class="layui-input-inline">
                                    <select name="isUsed">
                                        <option value=""  ${empty couponCode.isUsed ? 'selected' : ''}>全部</option>
                                        <option value="1" ${couponCode.isUsed == 1 ? 'selected' : ''}>已领取</option>
                                        <option value="2" ${couponCode.isUsed == 2 ? 'selected' : ''}>未领取</option>
                                    </select>
                                </div>
                            </li>
                            <li class="form-item-inline"><label class="search-form-lable">消费状态</label>
                                <div class="layui-input-inline">
                                    <select name="isSpending">
                                        <option value=""  ${empty couponCode.isSpending ? 'selected' : ''}>全部</option>
                                        <option value="1" ${couponCode.isSpending == 1 ? 'selected' : ''}>已消费</option>
                                        <option value="2" ${couponCode.isSpending == 2 ? 'selected' : ''}>未消费</option>
                                    </select>
                                </div>
                            </li>
                        </ul>
                        <ul class="search-form-content">
                            <li class="form-item"><label class="search-form-lable">创建时间</label>
                                <div class="check-btn-inner" id="timeType">
                                    <a id="all" href="javascript:void(0);"
                                       onclick="setTimeType($(this),0,'#myForm')" ${empty input.timeType || input.timeType == 0 ? 'class="active"' : ''}>全部</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),1,'#myForm')" ${input.timeType == 1 ? 'class="active"' : ''}>今天</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),2,'#myForm')" ${input.timeType == 2 ? 'class="active"' : ''}>本周内</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),3,'#myForm')" ${input.timeType == 3 ? 'class="active"' : ''}>本月内</a>
                                    <input type="hidden" name="timeType" value="${input.timeType}"/>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <input class="layui-input" type="text" name="createStart" value="${input.createStart}" placeholder="开始日">
                                    </div>
                                    -
                                    <div class="layui-input-inline">
                                        <input class="layui-input" type="text" name="createEnd" value="${input.createEnd}" placeholder="截止日">
                                    </div>
                                </div>
                            </li>
                            <li class="form-item-inline">
                                <div class="sub-btns">
                                    <a class="layui-btn layui-btn-danger" href="javascript:submitFunction('#myForm')">查询</a>
                                    <a class="layui-btn layui-btn-normal" href="javascript:resetFunction('#myForm')">重置</a>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </form>
            <div class="my-act-list-content">
                <ul class="num">
                    <div class="r tr">
                        <li>
                            <a class="layui-btn layui-btn-danger layui-btn-sm"
                               href="javascript:openDialog('新增核销码','${ctx}/coupon/couponCode/toForm.do?couponId=${coupon.id}','300px','200px')">新增核销码</a>
                        </li>
                        <%--<li>
                            <a class="layui-btn layui-btn-danger layui-btn-sm" id="btnImport">导入EXCEL</a>
                        </li>--%>
                        <li>
                            <a class="layui-btn layui-btn-danger layui-btn-sm" href="javascript:batchVerification()">批量核销</a>
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-danger layui-btn-sm" href="javascript:batchAdd()">批量增加</a>
                        </li>
                        <li>
                            <a class="layui-btn layui-btn-danger layui-btn-sm" id="btnExport">导出EXCEL</a>
                        </li>
                    </div>
                    <p class="cl"></p>
                </ul>
                <div class="cl">
                    <table class="layui-table">
                        <colgroup>
                            <col>
                            <col>
                            <col>
                            <col>
                            <col>
                            <col>
                        </colgroup>
                        <thead>
                        <tr>
                            <th>核销码</th>
                            <th>创建时间</th>
                            <th>是否领取</th>
                            <th>是否消费</th>
                            <th>消费日期</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach var="couponCode" items="${couponCodes}">
                            <tr>
                                <td>${couponCode.verifyCode}</td>
                                <td><fmt:formatDate value="${couponCode.createDate}" pattern="yyyy-MM-dd HH:mm"/></td>
                                <td>
                                    <c:if test="${couponCode.isUsed == 1}"><div class="red">已领取</div></c:if>
                                    <c:if test="${couponCode.isUsed == 2}"><div>未领取</div></c:if>
                                </td>
                                <td>
                                    <c:if test="${couponCode.isSpending == 1}"><div class="red">已消费</div></c:if>
                                    <c:if test="${couponCode.isSpending == 2}"><div>未消费</div></c:if>
                                </td>
                                <td>
                                    <c:if test="${couponCode.isSpending == 1 && not empty couponCode.spendingTime}">
                                        <fmt:formatDate value="${couponCode.spendingTime}" pattern="yyyy-MM-dd HH:mm"/>
                                    </c:if>
                                </td>
                                <td class="opts-btns tb-opts">
                                    <div class="comm-opts">
                                        <c:if test="${couponCode.isUsed == 2}">
                                            <a class="green" href="javascript:openDialog('编辑核销码','${ctx}/coupon/couponCode/toForm.do?couponId=${coupon.id}&codeId=${couponCode.id}','300px','200px')">编辑</a>
                                            <a class="red" href="javascript:txz.deleteObject('确定要删除该核销码吗？', '${ctx}/coupon/couponCode/delete.do?codeId=${couponCode.id}', '该券已有人领取，不能删除');">删除</a>
                                        </c:if>
                                    </div>
                                </td>
                            </tr>
                        </c:forEach>
                        <c:if test="${page.totalCount == 0}">
                            <tr>
                                <td colspan="6" class="f16 tc mt15">还没有核销码</td>
                            </tr>
                        </c:if>
                        </tbody>
                    </table>
                </div>
            </div>
            <div id="page_content" class="page-container"></div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp" %>
    </section>
    <div id="bmDialog">
        <form class="layui-form p10" action="${ctx}/coupon/couponCode/randomInsert.do" id="infoForm">
            <input type="hidden" name="couponId" value="${coupon.id}"/>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 30px;padding-left: 0px;">数量<span class="f-verify-red">*</span></label>
                <div class="layui-input-block" style="margin-left: 50px;">
                    <input type="number" name="count" min="1" class="layui-input" />
                </div>
            </div>
        </form>
    </div>
    <div id="verificationDialog">
        <form class="layui-form p10" action="${ctx}/coupon/couponCode/batchVerify.do" id="verificationForm">
            <div id="msg"></div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 50px;padding-left: 0px;">核销码<span
                            class="f-verify-red">*</span></label>
                    <div class="layui-input-inline" style="margin-bottom: 10px;">
                        <input type="number" name="verifyCode" min="1" class="layui-input"/>
                    </div>
                    <div class="layui-input-inline" style="width: 30px;height: 38px;line-height: 38px;">
                        <i class="iconfont icon-add btn-icon"></i>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/html" id="item_add_template">
    <div class="layui-inline">
        <label class="layui-form-label" style="width: 50px;padding-left: 0px;">
            核销码<span class="f-verify-red">*</span>
        </label>
        <div class="layui-input-inline" style="margin-bottom: 10px;">
            <input type="number" name="verifyCode" min="1" class="layui-input">
        </div>
        <div class="layui-input-inline" style="width: 30px;height: 38px;line-height: 38px;">
            <i class="iconfont icon-add btn-icon"></i>
        </div>
    </div>
</script>
<script type="text/html" id="item_close_template">
    <div class="layui-inline">
        <div class="layui-input-inline" style="margin-left: 65px; margin-bottom: 10px;">
            <input type="number" name="verifyCode" min="1" class="layui-input">
        </div>
        <div class="layui-input-inline" style="width: 30px;height: 38px;line-height: 38px;">
            <i class="iconfont icon-close btn-icon"></i>
        </div>
    </div>
</script>
<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript" src="${ctx}/script/common/table_option.js"></script>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '票券管理',
            href: '${ctx}/coupon/coupon/list.do'
        }, {
            name: '${coupon.name}'
        }, {
            name: '核销码管理',
            curr: true
        }],
        info: '<span class="f12">共<b>${page.totalCount}</b>条记录',
        btns: [{
            type: 'back'
        }]
    });

    var laytpl = null;
    $(function () {
        layui.use(['laydate', 'laytpl'], function () {
            var laydate = layui.laydate;
            laytpl = layui.laytpl;

            // 查询发布日期
            var start = {
                elem: 'input[name=createStart]'
            };

            var end = {
                elem: 'input[name=createEnd]'
            };
            laydate.render(start);
            laydate.render(end);
        });
        //加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });

        $("#btnExport").click(function () {
            layer.confirm('确认要导出Excel吗?', {
                icon: 3,
                title: '系统提示'
            }, function (index) {
                var url = "${ctx}/coupon/couponCode/exportCouponCode.do?couponId=${coupon.id}";
                var loadIndex = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                $.post(url, $("#myForm").serialize(), function (res) {
                    layer.close(loadIndex);
                    if (res.success) {
                        window.location.href = res.data;
                    } else {
                        util.layerMsgError(res.description)
                    }
                });
                layer.close(index);
            });
        });

        // 增加
        $("#verificationDialog").delegate(".icon-add", "click", function (e) {
            var getTpl = $("#item_close_template").html();
            laytpl(getTpl).render({}, function (html) {
                $("#verificationDialog").find(".layui-form-item").append(html);
            });
        });

        // 删除
        $("#verificationDialog").delegate(".icon-close", "click", function (e) {
            var $target = $(e.target);
            $target.closest(".layui-inline").remove();
        })
    });

    function openDialog(title, url, width, height, target) {
        layer.open({
            type: 2,
            area: [width, height],
            title: title,
            maxmin: true, //开启最大化最小化按钮
            content: url,
            btn: ['确定', '关闭'],
            yes: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var inputForm = body.find('#inputForm');
                var top_iframe;
                if (target) {
                    top_iframe = target;//如果指定了iframe，则在改frame中跳转
                } else {
                    top_iframe = '_parent';//获取当前active的tab的iframe
                }
                inputForm.attr("target", top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示

                if (iframeWin.contentWindow.doSubmit()) {
                    setTimeout(function () {
                        top.layer.close(index);
                    }, 100);//延时0.1秒，对应360 7.1版本bug

                    setTimeout(function () {
                        window.location.reload();
                    }, 2000);
                }
            },
            cancel: function (index) {
            }
        });
    }

    // 批量生成核销码
    function batchAdd() {
        var dialogIndex = layer.open({
            type: 1,
            area: '300px',
            title: '批量增加核销码',
            content: $("#bmDialog"),
            btn: ['确定', '取消'],
            success: function () { // 弹出后回调
                $("#infoForm").find("[name=count]").val("");
            },
            end: function () { // 销毁后回调
                $("#verificationDialog").find("[name=verifyCode]").val("");
            },
            yes: function () { // 确定按钮
                var count = $("#infoForm [name=count]").val();
                if (count == "" || parseInt(count) == 0) {
                    util.layerMsgError("请输入数量")
                } else {
                    //loading层
                    var loadIndex = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    var action = $("#infoForm").attr("action");
                    $.post(action, $('#infoForm').serialize(), function (res) {
                        layer.close(loadIndex);
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                submitFunction('#myForm');
                            });
                        } else {
                            util.layerMsgError("提交失败")
                        }
                    });
                    layer.close(dialogIndex);
                }
            }
        });
    }

    function resetDialog() {
        var getTpl = $("#item_add_template").html();
        laytpl(getTpl).render({}, function (html) {
            $("#verificationDialog").find(".layui-form-item").html(html);
        });
        $("#verificationDialog #msg").html('');
        $("#verificationDialog #msg").css('margin-bottom', '0px');
    }

    // 批量核销
    function batchVerification() {
        var dialogIndex = layer.open({
            type: 1,
            area: ['400px', '600px'],
            title: '批量核销',
            content: $("#verificationDialog"),
            btn: ['核销', '关闭'],
            success: resetDialog, // 弹出后回调
            end: function () { // 销毁后回调

            },
            yes: function () { // 确定按钮
                var array = new Array();
                $("#verificationDialog .layui-form-item").find(".layui-inline").each(function (index, ele) {
                    var value = $(ele).find("[name=verifyCode]").val();
                    if (util.isValid(value)) {
                        array.push(value);
                    }
                });

                if (array.length == 0) {
                    util.layerMsgError("请输入核销码")
                } else {
                    var confirmIndex = layer.confirm('核销后将变为已消费，是否确认核销？', {
                        icon: 3,
                        title: '系统提示'
                    }, function (index) {
                        //loading层
                        var loadIndex = layer.load(1, {
                            shade: [0.1, '#fff'] //0.1透明度的白色背景
                        });
                        var action = $("#verificationForm").attr("action");
                        $.post(action, {
                            verifyCodes: array.join(','),
                            couponId: '${coupon.id}'
                        }, function (res) {
                            layer.close(confirmIndex);
                            layer.close(loadIndex);
                            if (res.success) {
                                util.layerMsgSuccess("核销成功", function () {
                                    submitFunction('#myForm');
                                    layer.close(dialogIndex);
                                });
                            } else {
                                if (util.isValid(res.description)) {
                                    $("#verificationDialog #msg").html('');
                                    $("#verificationDialog #msg").html('<p class="error-class">' + res.description + '</p>');
                                    $.map(res.data, function (value, key) {
                                        $("#verificationDialog .error-class").append('<p>' + key + "  :  " + value + '</p>');
                                    })
                                    $("#verificationDialog #msg").css('margin-bottom', '15px');
                                } else {
                                    util.layerMsgError("核销失败");
                                }
                            }
                        });
                    })
                }
            },
            cancel: resetDialog,
            btn2: resetDialog
        });
    }
</script>
</body>
</html>